package org.chnxi.study.springcloud.dubbo.dto;

import lombok.*;

import java.time.LocalDateTime;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

/**
 * RPC通信时使用的请求基础对象
 * @param <T>
 */
@AllArgsConstructor
@NoArgsConstructor
@ToString
@Getter
@Setter
public class RpcRequest<T> extends AbstractRpcMessage implements java.io.Serializable {

    /**
     * 服务调用发起方信息
     */
    private ApplicationDescrib sender;

    /**
     * 服务性能数据信息，请求发送时，只需要填充requestTimestamp
     */
    private PerformanceDescrib performance;

    /**
     * 发送请求时携带的业务数据
     */
    private T busiData;

    /**
     * 创建一个新的请求对象，将会设置新的请求交易信息
     * @param t
     * @param <T>
     * @return
     */
    public static <T> RpcRequest newInstance(T t){
        RpcRequest<T> request = new RpcRequest<>();
        request.setTransaction(TransactionDescrib.newInstance4Request());
        request.setSender(ApplicationDescrib.getInstance());
        request.setPerformance(PerformanceDescrib.newInstance4Request());
        return request;
    }

    /**
     * 根据现有请求对象创建新的请求对象，将会保留原有的请求交易信息
     * @param request
     * @param <T>
     * @param <K>
     * @return
     */
    public static <T,K> RpcRequest<T> newInstanceWithRequest(RpcRequest<K> request, T reqArgs){
        RpcRequest<T> newReq = new RpcRequest<>();
        request.setTransaction(TransactionDescrib.newTnstance4Request(request.getTransaction()));
        request.setSender(ApplicationDescrib.getInstance());
        request.setPerformance(PerformanceDescrib.newInstance4Request());
        newReq.setBusiData(reqArgs);
        return newReq;
    }
}
